import ReactDOM from 'react-dom/client'
import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom'
import { ConfigProvider } from 'antd'
import zhCN from 'antd/locale/zh_CN'
import enUS from 'antd/locale/en_US'
import { useTranslation } from 'react-i18next'
import LandingPage from './pages/landing/index.tsx'
import Login from './pages/auth/Login.tsx'
import Signup from './pages/auth/Signup.tsx'
import NavigationPage from './pages/navigation'
import AdminPage from './pages/admin/index.tsx'
import ChatPage from './pages/ai/ChatPage.tsx'
import UnifiedProtectedRoute from './components/auth/UnifiedProtectedRoute'
import UnifiedAuthProvider from './components/auth/UnifiedAuthProvider'
import ErrorBoundary from './components/ErrorBoundary'
import { initializePWA } from './utils/pwa'
import { clearThemeCache } from './utils/clearThemeCache'
import { checkCurrentTheme } from './utils/checkTheme'
import { initializeDebugTools, handleRefreshLoop } from './utils/debugTools'
import './i18n'
import './styles/index.css'
import './styles/iconfont.css'
import './styles/antd-theme.css'

// 在开发模式下导入清理工具
if (process.env.NODE_ENV === 'development') {
  import('./utils/clearCache');
  import('./utils/devTools');
}

// 初始化调试工具
initializeDebugTools();

// 检测并处理刷新循环
handleRefreshLoop();

// 初始化 PWA
initializePWA().catch(console.error);

// 设置初始主题
const initializeTheme = () => {
  // 确保在浏览器环境中执行
  if (typeof window !== 'undefined') {
    // 检查是否需要清除主题缓存
    const urlParams = new URLSearchParams(window.location.search);
    if (urlParams.has('clearThemeCache')) {
      clearThemeCache();
      return;
    }
    
    try {
      // 获取存储的主题状态
      const storedTheme = localStorage.getItem('theme-storage');
      let themeMode = 'dark'; // 默认主题
      
      if (storedTheme) {
        const themeData = JSON.parse(storedTheme);
        themeMode = themeData.state?.mode || 'dark';
      }
      
      // 设置DOM属性
      document.documentElement.setAttribute('data-theme', themeMode);
      
      // 如果需要，可以在这里初始化主题存储状态
      // 但这应该由zustand的持久化中间件处理
    } catch (e) {
      console.error('Failed to parse theme storage:', e);
      // 默认设置为深色主题
      document.documentElement.setAttribute('data-theme', 'dark');
    }
  } else {
    // 服务端渲染时默认设置为深色主题
    document.documentElement.setAttribute('data-theme', 'dark');
  }
};

// 初始化主题
initializeTheme();

// 检查当前主题设置
checkCurrentTheme();

// App 组件包装器，用于处理国际化
function AppWrapper() {
  const { i18n } = useTranslation();
  const antdLocale = i18n.language === 'en-US' ? enUS : zhCN;

  return (
    <ErrorBoundary>
      <ConfigProvider locale={antdLocale}>
        <UnifiedAuthProvider>
          <BrowserRouter>
            <Routes>
              <Route path="/" element={<LandingPage />} />
              <Route path="/login" element={<Login />} />
              <Route path="/signup" element={<Signup />} />
              <Route 
                path="/dashboard" 
                element={
                  <UnifiedProtectedRoute>
                    <NavigationPage />
                  </UnifiedProtectedRoute>
                } 
              />
              <Route 
                path="/hot" 
                element={
                  <UnifiedProtectedRoute>
                    <NavigationPage />
                  </UnifiedProtectedRoute>
                } 
              />
              <Route 
                path="/crawler" 
                element={
                  <UnifiedProtectedRoute>
                    <NavigationPage />
                  </UnifiedProtectedRoute>
                } 
              />
              <Route 
                path="/crawler/tasks" 
                element={
                  <UnifiedProtectedRoute>
                    <NavigationPage />
                  </UnifiedProtectedRoute>
                } 
              />
              <Route 
                path="/crawler/scheduler" 
                element={
                  <UnifiedProtectedRoute>
                    <NavigationPage />
                  </UnifiedProtectedRoute>
                } 
              />
              <Route 
                path="/crawler/data" 
                element={
                  <UnifiedProtectedRoute>
                    <NavigationPage />
                  </UnifiedProtectedRoute>
                } 
              />
              <Route 
                path="/crawler/config" 
                element={
                  <UnifiedProtectedRoute>
                    <NavigationPage />
                  </UnifiedProtectedRoute>
                } 
              />
              <Route 
                path="/crawler/monitor" 
                element={
                  <UnifiedProtectedRoute>
                    <NavigationPage />
                  </UnifiedProtectedRoute>
                } 
              />
              <Route 
                path="/crawler/logs" 
                element={
                  <UnifiedProtectedRoute>
                    <NavigationPage />
                  </UnifiedProtectedRoute>
                } 
              />
              <Route 
                path="/crawler/overview" 
                element={
                  <UnifiedProtectedRoute>
                    <NavigationPage />
                  </UnifiedProtectedRoute>
                } 
              />
              <Route 
                path="/scheduler" 
                element={
                  <UnifiedProtectedRoute>
                    <NavigationPage />
                  </UnifiedProtectedRoute>
                } 
              />
              <Route 
                path="/analytics" 
                element={
                  <UnifiedProtectedRoute>
                    <NavigationPage />
                  </UnifiedProtectedRoute>
                } 
              />
              <Route 
                path="/users" 
                element={
                  <UnifiedProtectedRoute>
                    <NavigationPage />
                  </UnifiedProtectedRoute>
                } 
              />
              <Route 
                path="/files" 
                element={
                  <UnifiedProtectedRoute>
                    <NavigationPage />
                  </UnifiedProtectedRoute>
                } 
              />
              <Route 
                path="/search" 
                element={
                  <UnifiedProtectedRoute>
                    <NavigationPage />
                  </UnifiedProtectedRoute>
                } 
              />
              <Route 
                path="/notifications" 
                element={
                  <UnifiedProtectedRoute>
                    <NavigationPage />
                  </UnifiedProtectedRoute>
                } 
              />
              <Route 
                path="/profile" 
                element={
                  <UnifiedProtectedRoute>
                    <NavigationPage />
                  </UnifiedProtectedRoute>
                } 
              />
              <Route 
                path="/test" 
                element={
                  <UnifiedProtectedRoute>
                    <NavigationPage />
                  </UnifiedProtectedRoute>
                } 
              />
              <Route 
                path="/simple-test" 
                element={
                  <UnifiedProtectedRoute>
                    <NavigationPage />
                  </UnifiedProtectedRoute>
                } 
              />
              <Route 
                path="/theme-test" 
                element={
                  <UnifiedProtectedRoute>
                    <NavigationPage />
                  </UnifiedProtectedRoute>
                } 
              />
              <Route 
                path="/admin" 
                element={
                  <UnifiedProtectedRoute>
                    <AdminPage />
                  </UnifiedProtectedRoute>
                } 
              />
              <Route 
                path="/admin/dashboard" 
                element={
                  <UnifiedProtectedRoute>
                    <AdminPage />
                  </UnifiedProtectedRoute>
                } 
              />
              <Route 
                path="/admin/users" 
                element={
                  <UnifiedProtectedRoute>
                    <AdminPage />
                  </UnifiedProtectedRoute>
                } 
              />
              <Route 
                path="/admin/roles" 
                element={
                  <UnifiedProtectedRoute>
                    <AdminPage />
                  </UnifiedProtectedRoute>
                } 
              />
              <Route 
                path="/admin/permissions" 
                element={
                  <UnifiedProtectedRoute>
                    <AdminPage />
                  </UnifiedProtectedRoute>
                } 
              />
              <Route 
                path="/admin/crawlers" 
                element={
                  <UnifiedProtectedRoute>
                    <AdminPage />
                  </UnifiedProtectedRoute>
                } 
              />
              <Route 
                path="/admin/system" 
                element={
                  <UnifiedProtectedRoute>
                    <AdminPage />
                  </UnifiedProtectedRoute>
                } 
              />
              <Route 
                path="/ai/chat" 
                element={
                  <UnifiedProtectedRoute>
                    <NavigationPage />
                  </UnifiedProtectedRoute>
                } 
              />
              <Route path="*" element={<Navigate to="/" replace />} />
            </Routes>
          </BrowserRouter>
        </UnifiedAuthProvider>
      </ConfigProvider>
    </ErrorBoundary>
  );
}

ReactDOM.createRoot(document.getElementById('root')!).render(<AppWrapper />)